package itcast.web;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




/**
 * Servlet implementation class FcheckcodeServlet
 */
@WebServlet("/fcheckcodeServlet")
public class FcheckcodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FcheckcodeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		int width=100;
		int height=35;
		Random ran=new Random();//随机对象
		
		//创建一个对象，图片对象
		BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
		//2.美化图片
		//2.1填充背景颜色
		Graphics g =image.getGraphics();//定义画笔对象
		g.setColor(randomColor());//设置画笔颜色
		g.fillRect(0, 0, width, height);
		//2.2画边框
		g.setColor(Color.BLUE);
		g.drawRect(0, 0, width-1, height-1);
		
		//测试起来太费力写0-9
		String str="01";
		//String str="0123456789";
		//String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		StringBuilder sb=new StringBuilder();
		for (int i = 1; i <=4; i++) {
			int index=ran.nextInt(str.length());//获取字符
			char ch =str.charAt(index);//获取随机字符每一次获取一个
			g.setFont(randomFont());//随机字体
			g.drawString(ch+" ",width/5*i-3, height/2+10);
			sb.append(ch);
		}
		String checkcode= sb.toString();
		request.getSession().setAttribute("checkcode",checkcode);
		//随机生成坐标点来画线
		g.setColor(randomColor());
		for (int i = 0; i < 5; i++) {
			int x1=ran.nextInt(width);
			int x2=ran.nextInt(width);
			int y1=ran.nextInt(width);
			int y2=ran.nextInt(width);
			g.drawLine(x1, y1, x2, y2);
		}
		//画干扰线
		
		//3、将图片输出到页面展示
		ImageIO.write(image,"jpg",response.getOutputStream());
	}
		private Color randomColor(){
		 Random ran=new Random();//随机对象
	        int red = ran.nextInt(255);
	        int green = ran.nextInt(255);
	        int blue = ran.nextInt(255);
	        return new Color(red,green,blue);
	    }
	 	
		private Font randomFont(){
	    	Random ran=new Random();//随机对象
	        String[] fontNames= {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"};
	        int index = ran.nextInt(fontNames.length);
	        String fontName = fontNames[index];
	        int style = ran.nextInt(4);
	        int size=ran.nextInt(5)+ 24;
	        return new Font(fontName,style,size);
	    }
		

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	
	}

}
